define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jy/service/index' + location.search,
                    add_url: 'jy/service/add',
                    edit_url: 'jy/service/edit',
                    del_url: 'jy/service/del',
                    multi_url: 'jy/service/multi',
                    import_url: 'jy/service/import',
                    table: 'service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'update_time',
                search: false,
                undefinedText: '',
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                    ignoreColumn: ['operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    if (filter['servicetype.type_name']) {
                        filter['servicetype.id'] = filter['servicetype.type_name'];
                        op['servicetype.id'] = op['type_name'];
                        delete filter['servicetype.type_name'];
                        delete op['servicetype.type_name'];
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'service_name', title: __('Service_name'), operate: 'LIKE'},
                        {field: 'sub_name', title: __('Sub_name'), operate: 'LIKE', visible: false},
                        {
                            field: 'servicetype.type_name',
                            title: __('Servicetype.type_name'),
                            addClass: "selectpage",
                            extend: "data-source='jy/service_type/index' data-field='type_name'",
                            cellStyle: {
                                css: {
                                    "max-width": "300px",
                                    "white-space": "normal"
                                }
                            }
                        },
                        {
                            field: 'cover_image',
                            title: __('Cover_image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'effect_images',
                            title: __('Effect_images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'item_title', title: __('Item_title'), operate: false},
                        {field: 'scan_count', title: __('Scan_count'), operate: false, sortable: true, visible: false},
                        {
                            field: 'scan_user_count',
                            title: __('Scan_user_count'),
                            operate: false,
                            sortable: true,
                            visible: false
                        },
                        {
                            field: 'share_count',
                            title: __('Share_count'),
                            operate: false,
                            sortable: true,
                            visible: false
                        },
                        {
                            field: 'share_user_count',
                            title: __('Share_user_count'),
                            operate: false,
                            sortable: true,
                            visible: false
                        },
                        {field: 'weigh', title: __('Weigh'), sortable: true, operate: false},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {"1": __('Status 1'), "2": __('Status 2'), "0": __('Status 0')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'create_user', title: __('Create_user'), operate: false, visible: false},
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            sortable: true,
                            autocomplete: false,
                            operate: false,
                        },
                        {field: 'update_user', title: __('Update_user'), operate: false, visible: false},
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            sortable: true,
                            autocomplete: false,
                            operate: false,
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            switchable: false,
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'tender',
                                    text: '服务规格',
                                    icon: '',
                                    classname: 'btn btn-xs btn-dialog btn-info',
                                    title: function (value) {
                                        return value.service_name;
                                    },
                                    extend: 'data-area=["1500px","800px"]',
                                    url: function (value) {
                                        return 'jy/service_paving_type/index?serviceId=' + value.id;
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});